﻿// JavaScript Document

(function(){
	$.fn.drawForm = function(options){
		var opts = options;
		var str="";	
		for(var i = 0;i<opts.length;i++){
			var name = opts[i].name,
				type = opts[i].type,
				controlName = opts[i].controlName,
				width = opts[i].width,
				checkLau = opts[i].check,
				patten = opts[i].patten;
			
			str+="<div class='form-group'>"
			   +"<label>"+name+"：</label>"
			if(jsonList[i].type=="select"){
				var defaultSel = jsonList[i].defaultSel,
					option = jsonList[i].option;
				str+="<select name='"+controlName+"' id='"+controlName+"' patten='"+patten+"' isValue='"+defaultSel+"' class='form-control form-select'"
				   +"style='width:"+width+"px'>";
				for(var j = 0;j<option.length;j++){
					str+="<option>"+option[j]+"</option>"
				}
				str+="</select>";
			}else{
				str+="<input type='"+type+"' patten='"+patten+"' id='"+controlName+"' name = '"+controlName+"' class='form-control'"
				   +"style='width:"+width+"px'>"
			}
			str+="<span>"+checkLau+"</span></div>";
		}
		str+="<div class='form-group'><button type='submit' class='btn btn-primary'><i class='icon-check'></i>&nbsp;提交</button></div>"
		$(this).html(str);
		$.each($("select"),function(){
			$(this).val($(this).attr("isValue"));
		});
		$("select,input").on("blur",function(){
			var val = $(this).val();
			if($(this).attr("patten")==="isNull" &&( val==="" || val === null)){
				$(this).next("span").text($(this).prev("label").text()+"不能为空");	
			}
		}).on("focus",function(){
			$(this).next("span").text("");
		})
	}
	
})(jQuery)